Set up online repositories for data such as Google Drive and retrieve it directly into RStudio for utilization.
Describe how the Grammar of Graphics deviates from built-in plotting commands.
Use an aes-thetic to define which data components of the data will be used in constructing the graphic.
Apply geommetric data layers to existing plots.
For this example, we will use the Rice Rivers Center data again.
A aesthetic statement indicating which columns of data to use and how to use them in the plot (designating x-axis vs color, etc.).
An estimate of a trendline through the data (the red one), which displays a statistical summary of the raw data.
A set of geometric overlays for the points which include size and shape configurations.
Specified color scheme for the regions.
Labeling of a subset of the data (which is done using a separate data.frame derived from the first).
Labels on axes.
A legend positioned in a specific fashion.
A title over the whole thing.
A theme for the rest of the coloring and customized lines and grids.
Data
Aesthetics
Transformations
Partitions
Auxillary Text
Overlays
ggplot2 LibraryAn aesthetic is a function that allows you to tell the graphics which columns of data are to be used in the creation of graph features.
Aesthetic mapping:
* `x` -> `Sepal.Length`
* `y` -> `Sepal.Width`
Adding a Geometry Layer
Aesthetics also contribute to symbologies and colors
aes()pOnly things in ggplot() apply to all following components. Placing aes() or data= parts in later components only make them visible to that particular component.
function (base_size = 18, base_family = "")
{
theme_grey(base_size = base_size, base_family = base_family) %+replace%
theme(axis.line = element_blank(), axis.text.x = element_text(size = base_size *
0.8, color = "white", lineheight = 0.9), axis.text.y = element_text(size = base_size *
0.8, color = "white", lineheight = 0.9), axis.ticks = element_line(color = "white",
size = 0.2), axis.title.x = element_text(size = base_size,
color = "white", margin = margin(0, 10, 0, 0)), axis.title.y = element_text(size = base_size,
color = "white", angle = 90, margin = margin(0, 10,
0, 0)), axis.ticks.length = unit(0.3, "lines"),
legend.background = element_rect(color = NA, fill = "#272822"),
legend.key = element_rect(color = "white", fill = "#272822"),
legend.key.size = unit(1.2, "lines"), legend.key.height = NULL,
legend.key.width = NULL, legend.text = element_text(size = base_size *
0.8, color = "white"), legend.title = element_text(size = base_size *
0.8, face = "bold", hjust = 0, color = "white"),
legend.position = "right", legend.text.align = NULL,
legend.title.align = NULL, legend.direction = "vertical",
legend.box = NULL, panel.background = element_rect(fill = "#272822",
color = NA), panel.border = element_rect(fill = NA,
color = "white"), panel.grid.major = element_line(color = "grey35"),
panel.grid.minor = element_line(color = "grey20"),
panel.spacing = unit(0.5, "lines"), strip.background = element_rect(fill = "grey30",
color = "grey10"), strip.text.x = element_text(size = base_size *
0.8, color = "white"), strip.text.y = element_text(size = base_size *
0.8, color = "white", angle = -90), plot.background = element_rect(color = "#272822",
fill = "#272822"), plot.title = element_text(size = base_size *
1.2, color = "white"), plot.margin = unit(rep(1,
4), "lines"))
}
You can also set the theme and base sizes (and other theme-related items) globally for an entire document/presentation as:
Customizing the y-axis data format…
Pearson's product-moment correlation
data: iris$Sepal.Length and iris$Sepal.Width
t = -1.4403, df = 148, p-value = 0.1519
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.27269325 0.04351158
sample estimates:
cor
-0.1175698
sLength <- by( iris$Sepal.Length, iris$Species, mean )
sWidth <- by( iris$Sepal.Width, iris$Species, mean )
df <- data.frame( Sepal.Length = as.numeric( sLength ),
Sepal.Width = as.numeric( sWidth ),
Species = levels( iris$Species ) )
df Sepal.Length Sepal.Width Species
1 5.006 3.428 setosa
2 5.936 2.770 versicolor
3 6.588 2.974 virginica